package com.heima.domain;

import com.alibaba.fastjson.JSON;
import com.heima.bean.User;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//用户注册
public class Register {
    //创建用户集合
    private static List<User> list = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    public void userRegister() throws IOException {
        //创建用户对象
        User user = new User();
        //打印控制台提示信息
        String username = null;
        String password = null;
        //判断用户名不为空
        while (true) {
            System.out.println("请输入用户名：");
            username = sc.nextLine();
            //获取json数据
            if(username.length()> 0){
                break;
            }
        }
        //判断两次密码一致；
        while (true) {
            System.out.println("请输入密码：");
            password = sc.nextLine();
            System.out.println("请确认输入密码：");
            String againPassword = sc.nextLine();
            if(password.equals(againPassword)){
                break;
            }else {
                System.out.println("确认密码不一致，请重新输入");
            }
        }
        user.setName(username);
        user.setPassword(password);
        //添加用户到集合
        list.add(user);
        //转成json字符
        String usersString = JSON.toJSONString(list);
        //创建写入流，写入本地；
        BufferedWriter bw = new BufferedWriter(new FileWriter("novelBrowse\\files\\user\\usermessages.txt"));
        bw.write(usersString);
        bw.flush();
        bw.close();
    }
}
